<?php

class RoleinfoController extends Zend_Controller_Action
{
	private $_auth;
	private $_acl;
    private $_params;
    private $_memcache;
    private $_my_mem;
    public function init(){
        $acl                = new My_Acl();
        $this->_acl         = $acl;
        $auth               = $acl->checkAllowed($this->_request);
        $this->_auth        = $auth->getIdentity();
        $this->view->auth   = $this->_auth;
        $this->_acl         = $acl;
        $this->view->acl    = $this->_acl;
        $this->_params      = $this->_request->getParams();
        
        $memcache = new Memcache;
        $memcache->connect('localhost', MEMCACHE_PORT) or die ("Could not connect");
        $this->_memcache = $memcache;
        $_my_mem = new My_Mem();
        $this->_my_mem = $_my_mem;
    }
    public function indexAction(){
        
        
        # get time start and time end of statistic
        $_params            = $this->_params;
        $gameId             = $_params['game'];
        //lay gameID
        $this->view->gameId = $gameId;
        //truyen qua view gameid
        $modelGame          = new Default_Model_Game();
        $gameCheck          = $modelGame->getDataById($gameId)->toArray();
        if($gameCheck){
            $this->view->gameCheck = $gameCheck;
            if(!file_exists(LIBRARY_PATH."/My/API/$gameId/Users.php")){
                $this->view->mess = "Không thể tra cứu thông tin nhân vật của {$gameCheck['gameName']}!";
                $this->view->flag = 'error';
                return;
            }
            $usersApi = "My_API_{$gameId}_Users";
            $usersApi = new $usersApi($gameId);    
            if(file_exists(CONFIG_PATH2 . '/'.$gameId.'.ini')){//kiem tra co ton tai config cua gameid k?
                $configGame2 = new Zend_Config_Ini(CONFIG_PATH2 . '/'.$gameId.'.ini', APPLICATION_ENV);
                if(!isset($configGame2->getUserInfo)){
                    $this->view->mess = 'Không thể tra cứu thông tin nhân vật của game '.$gameCheck['gameName'].'!';
                    $this->view->flag = 'alert';
                    return;
                }
            }
            //get server List       
            if(file_exists(CONFIG_PATH . '/'.$gameId.'.ini')){
                $configGame = new Zend_Config_Ini(CONFIG_PATH . '/'.$gameId.'.ini', APPLICATION_ENV);
                $modelRole          = new Default_Model_Role();
                $roleList           = $modelRole->getDataByUserIdGameId($this->_auth['userId'],$gameId)->toArray();
                $channelDisabled    = array();
                foreach($roleList as $row){
                    if(isset($row['channelDisabled']) && $row['channelDisabled']){
                        if(is_array(json_decode($row['channelDisabled']))){
                            $channelDisabled    = array_merge($channelDisabled,json_decode($row['channelDisabled']));
                        }
                    }
                } 
                $arrGameId=array('ss31', 'ss32');
                if(!in_array($gameId, $arrGameId)){//kiem tra neu la ss31 thi ko can cho chon channel
                
                    if(isset($configGame->channel)){
                        $channelGame = array();
                        foreach($configGame->channel->toArray() as $key=>$value){
                            $channelGame[] = $key;
                        }
                    }
                    else{
                        $this->view->mess = 'Không tim thay channel';
                        $this->view->flag = 'alert';
                        return;
                    }
                    $arrChannel       = $configGame->channel->toArray();
                   //lay danh sach thong tin channel bo vao maang arrchanenel
                    foreach($arrChannel as $key=>$row){
                        if(!isset($channelDisabled[0]) || (isset($channelDisabled[0]) && !in_array($key,$channelDisabled))){
                            
                        }else{
                            unset($arrChannel[$key]);
                        }
                    } 
                    //xoa di channel disable
                    $this->view->arrChannel = $arrChannel;
                
               //truyen list channel qua view
                    if(isset($configGame->server)){
                        $arrServer = $configGame->server->toArray();
                       //list server trong file config
                        $temp = array();
                        foreach($arrServer as $key=>$server){
                            if(isset($server['name'])){
                                $temp[$key] = $server['name'];
                            }
                        }
                        $arrServer = $temp;
                        $this->view->arrServer = $arrServer;
                    }
                }//end kiem tra game ss31
            }
            //da list xong server va channel
            if(file_exists(CONFIG_PATH2 . '/'.$gameId.'.ini')){
                $configGame = new Zend_Config_Ini(CONFIG_PATH2 . '/'.$gameId.'.ini', APPLICATION_ENV);
                if(isset($_params['bntSubmit']) && $_params['bntSubmit'] == 'Tìm'){
                    //neu nhan vao nut tim
                    $username = '';
                    $channel = '';
                    $serverId = '';
                    if(!isset($_params['username']) || !$_params['username']){
                        $this->view->mess = 'Vui lòng nhập username!';
                        $this->view->flag = 'alert';
                        return;
                    }
                    $username               = trim($_params['username']);
                    //cat no khoang trang dau va cuoi, 2 khoang trang lien tip
                    $this->view->username   = $username;
                    //kiem tra nguoi dung co chon server chua
                    if(isset($_params['serverId']) && $_params['serverId']){
                        $serverId               = $_params['serverId'];
                        if($arrServer && !isset($arrServer[$serverId])){
                            $this->view->mess = 'Không có dữ liệu!';
                            $this->view->flag = 'alert';
                            return;
                        }
                        $this->view->serverId   = $serverId;
                    }
                    //kiem tra nguoi dug co chon channel chua
                    if(isset($_params['channel']) && $_params['channel']){
                        $channel               = trim($_params['channel']);
                        $this->view->channel   = $channel;
                    }
                    # get data user
                    $getProfile = new My_API_getProfile();
                    //new class getprofile
                    //neu nhu la ss31
                    if(in_array($gameId, $arrGameId)){//neu game la ss31 va ss32 thi ko can chon channel cung nhu server
                        $dataUser=$getProfile->getdataUserID(array('userList'=>$username, 'gameId'=>$gameId));
                        //Array(thanhloc=>array(userId=1))
                        $userPlayGame = $username;
                        if(isset($configGame->getUserInfo->playgameBy) && $configGame->getUserInfo->playgameBy == 'userId'){
                            if(!isset($dataUser[$username]['userId'])){
                                $this->view->mess = "User {$username} không tồn tại!";
                                $this->view->flag = 'alert';
                                return;
                            }                    
                            $userPlayGame = $dataUser[$username]['userId'];                            
                        }
                        $dataRole = array(); 
                        $dataRole = $usersApi->getUsersByServerId($userPlayGame,$gameId);
                      
                        //array(roleName=>'thanhloc', info=>array(Object1(gameId: s112, level:1), Object1(gameId: s112, level:1)))
                        if(!$dataRole){
                            $this->view->mess = 'Không tìm thấy user '.$username.'!';
                            $this->view->flag = 'alert';
                            return;
                        } 
                        else{
                            if(isset($configGame->game->value) || isset($configGame->server)){
                                $arrGame = array();
                                $arrGame1=$configGame->server->toArray();
                                foreach($arrGame1 as $keyGame=>$valGame){
                                    $arrGame[$keyGame]=implode("", $valGame);
                                }
                                $title = $configGame->statistics->value->toArray();
                                $this->view->title=$title;
                                $this->view->listGame=$arrGame;
                                $this->view->dataRole = $dataRole;
                            }
                            else{
                                $this->view->mess = 'Vui lòng kiểm tra lại setting của game!';
                                $this->view->flag = 'alert';
                                return;
                            }
                        }
                    }
                    else{
                    //end xu ly ss31
                        if(!$channel){
                            foreach($arrChannel as $key=>$value){
                                
                                $dataUser[$key] = $getProfile->getdataUserID(array('userList'=>$username,'channel'=>$key, 'gameId'=>$gameId));                   
                           //buoc này sẽ chuyen tu username thanh userid
                            } 
                        }else{   
                            $dataUser[$channel] = $getProfile->getdataUserID(array('userList'=>$username,'channel'=>$channel, 'gameId'=>$gameId ));
                        }
                        
                        if(!isset($dataUser)){
                            $this->view->mess = "User {$username} không tồn tại!";
                            $this->view->flag = 'alert';
                            return;
                        }       
                    $userPlayGame = $username;
                    $dataRole = array(); 
                   
                    //buoc này sẽ get thong tin thong qua username hay userid tuy game                                     
                    if(!$channel){
                        foreach($arrChannel as $keyChannel => $valueChannel){//lap channel
                            if(isset($configGame->getUserInfo->playgameBy) && $configGame->getUserInfo->playgameBy == 'userId'){
                                if(!isset($dataUser[$keyChannel][$username]['userId'])){
                                    continue;
                                }                    
                                $userPlayGame = $dataUser[$keyChannel][$username]['userId'];                              
                            }                     
                            if(!$serverId){
                                //server.if.value='True'
                                if(isset($configGame->server->if->value) && $configGame->server->if->value == 'True'){
                                    $dataRole[$keyChannel] = $usersApi->getUsersByServerId(array($userPlayGame), $keyChannel);
                                }
                                else
                                {
                                    foreach($arrServer as $key=>$row){
                                       
                                        $result = $usersApi->getUsersByServerId(array($userPlayGame),$key, $keyChannel);  
                                        if($result){
                                            if(isset($dataRole[$keyChannel])){
                                                $dataRole[$keyChannel] = array_merge($dataRole[$keyChannel],$result);
                                        //tu lan 2 tro di se chay vao day, ghep 2 mang lai thanh 1
                                            }else{
                                                $dataRole[$keyChannel] = $result;
                                                //lan dau chay mang se vao day, luc nay datarole[keychannel] da co gia tri
                                            }
                                        }        
                                    }
                                }
                            
                            if(!$dataRole){
                                $this->view->mess = 'Không tìm thấy user '.$username.'!';
                                $this->view->flag = 'alert';
                                return;
                            } 
                            }else{//neu co chon server
                                
                                $result = $usersApi->getUsersByServerId(array($userPlayGame),$serverId, $keyChannel);
                                if($result){
                                    if(isset($dataRole[$keyChannel])){
                                        $dataRole[$keyChannel] = array_merge($dataRole[$keyChannel],$result);
                                    }else{
                                        $dataRole[$keyChannel] = $result;
                                    }
                                }
                            }
                        }
                        if(!$dataRole){
                            $this->view->mess = 'Không tìm thấy user '.$username.'!';
                            $this->view->flag = 'alert';
                            return;
                        }
                    }else{//neu co chon channel
                        if(isset($configGame->getUserInfo->playgameBy) && $configGame->getUserInfo->playgameBy == 'userId'){
                            if(!isset($dataUser[$channel][$username]['userId'])){
                                $this->view->mess = 'Không tìm thấy user '.$username.'!';
                                $this->view->flag = 'alert';
                                return;
                            }
                            //neu game dang choi doi tac mun nhan userid thi minh pai chuyen thanh id
                            $userPlayGame = $dataUser[$channel][$username]['userId'];
                         
                        }
                        if(!$serverId){//neu k chon server
                        if(isset($configGame->server->if->value) && $configGame->server->if->value == 'True'){//neu la sm05 thi k can truyen server
                            
                                 $dataRole[$channel] = $usersApi->getUsersByServerId(array($userPlayGame), $channel);
                            
                        }
                        else{
                            foreach($arrServer as $key=>$row){
                                $result = $usersApi->getUsersByServerId(array($userPlayGame),$key,$channel);
                                if($result){
                                    if(isset($dataRole[$channel])){
                                        $dataRole[$channel] = array_merge($dataRole[$channel],$result);
                                    }else{
                                        $dataRole[$channel] = $result;
                                    }
                                }
                            }
                        }
                            if(!$dataRole)
                            {
                                $this->view->mess = 'Không tìm thấy user '.$username.'!';
                                $this->view->flag = 'alert';
                                return;
                            }
                        }else{//neu co chon server
                            $result = $usersApi->getUsersByServerId(array($userPlayGame),$serverId,$channel); 
                            if($result){
                                $dataRole[$channel] = $result;
                            }
                            else{
                                $this->view->mess = 'Không tìm thấy user '.$username.'!';
                                $this->view->flag = 'alert';
                                return;
                            }
                        }
                    } 
                   if(isset($configGame->statistics->role))
                   {
                       $title = $configGame->statistics->role->toArray();
                       $this->view->title=$title;
                       $this->view->dataRole = $dataRole;
                       $this->view->dataRoleTitle=(current(current($dataRole)));
                   }
                   else
                   {
                       $this->view->mess = 'vui long kiểm tra lại setting của game !';
                       $this->view->flag = 'error';
                       $this->view->repair = 1;
                   }
                   
                }//end else ss31
            }//end submit
            }//end kiem tra ini
            else{
                $this->view->mess = 'Chưa có setting cho game!';
                $this->view->flag = 'error';
                $this->view->repair = 1;
            }
        }
    }//end function index
}
